const app = getApp();
Page({
	data: {
		url: app.globalData.host,
		tabList: [
			{name:"待使用",id:2},
			{name:"已完成",id:6},
			{name:"全部",id:''}
		],
		tabIndex: 0,
		tabId: 0,

		list: [],
		pageNum: 1,
		pageSize: 10,
		pageTotal: 0,
		isShowItem: false,	// 展开/收起

		isLoadBtn: false,	// 登录按钮

		code: null,
		token: null,
		infos: null,
	},
	onLoad(options) {
		wx.setNavigationBarTitle({
			title: "订单"
		})
	},
	onShow(){
		if(wx.getStorageSync("orderInd")){
			let ind = wx.getStorageSync("orderInd");
			let id = this.data.tabList[ind].id;
			this.setData({
				tabIndex: ind,
				tabId: id
			})
		}else{
			this.setData({
				tabIndex: 0,
				tabId: 2
			})
		}
		this.getUserInfo();
	},
	tabClick(e){
		let ind = e.currentTarget.dataset.index;
		let id = e.currentTarget.dataset.id;
		if(ind !== this.data.tabIndex){
			this.setData({
				tabIndex: ind,
				tabId: id
			})
			wx.setStorageSync("orderInd",ind);
			this.getList();
		}
	},
	// 从后台获取用户信息
	getUserInfo(){
		wx.showLoading({title: '加载中…'});
		app.request({
			url: '/app/user',
			method: 'GET',
			success: res => {
				if(res.code === 200){
					this.setData({
						list: [],
						isLoadBtn: false
					})
					this.getList();
				}else{
					this.setData({
						isLoadBtn: true
					})
				}
			}
		})
		wx.hideLoading();
	},
	getList(){
		wx.showLoading({title: '加载中…'});
		app.request({
			url: '/app/order',
			method: 'GET',
			data: {
				pageNum: this.data.pageNum,
				pageSize: this.data.pageSize,
				status: this.data.tabId
			},
			success: res => {
				let list = [];
				if(this.data.pageNum == 1){
					list = res.rows;
				}else{
					list = this.data.list.concat(res.rows);
				}
				if(list.length !== 0){
					list.forEach((item,index) => {
						item.isShow = false;	// 展示/隐藏 所需值
						if(item.status == 1){
							item.statusName = "待支付";
						}else if(item.status == 2){
							item.statusName = "待核销";
						}else if(item.status == 3){
							item.statusName = "已过期";
						}else if(item.status == 4){
							item.statusName = "退款中";
						}else if(item.status == 5){
							item.statusName = "已退款";
						}else if(item.status == 6){
							item.statusName = "已完成";
						}
						if(index == list.length - 1){
							this.setData({
								list: list
							})
						}
					})
				}else{
					this.setData({
						list: []
					})
				}
			}
		})
		wx.hideLoading();
	},
	// 收起
	toggle(e) {
		let ind = e.currentTarget.dataset.index;
		let value = !this.data.list[ind].isShow;
		this.setData({
			['list[' + ind + '].isShow']: value
		})
	},
	// 上拉加载
	onReachBottom(){
		let total = this.data.pageTotal;
		let num = this.data.pageNum;
		if(this.data.list.length < total){
			this.setData({
				pageNum: num + 1
			})
			this.getList();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		wx.showNavigationBarLoading() //在标题栏中显示加载
		app.request({
			url: '/app/order',
			method: 'GET',
			data: {
				pageNum: 1,
				pageSize: this.data.pageSize,
				status: this.data.tabId
			},
			success: res => {
				let list = [];
				list = res.data;
				if(list.length !== 0){
					list.forEach((item,index) => {
						item.isShow = false;	// 展示/隐藏 所需值
						if(item.status == 1){
							item.statusName = "待支付";
						}else if(item.status == 2){
							item.statusName = "待核销";
						}else if(item.status == 3){
							item.statusName = "已过期";
						}else if(item.status == 4){
							item.statusName = "退款中";
						}else if(item.status == 5){
							item.statusName = "已退款";
						}else if(item.status == 6){
							item.statusName = "已完成";
						}
						if(index == list.length - 1){
							this.setData({
								list: list
							})
						}
					})
				}else{
					this.setData({
						list: []
					})
				}
				this.setData({
					pageTotal: res.total,
					pageNum: 1
				})
			}
		})
		wx.hideNavigationBarLoading() //完成停止加载
		wx.stopPullDownRefresh() //停止下拉刷新
	},
	// button 获取
	getPhoneNumber (e) {
		wx.login({
			success: info => {
				let iv = e.detail.iv;
				let code = info.code;
				let encryptedData = e.detail.encryptedData;
				let infos = {
					iv: e.detail.iv,
					code: info.code,
					encryptedData: e.detail.encryptedData
				}
				//手机号和验证码验证略
				let errMsg = e.detail.errMsg;
				if(errMsg == 'getPhoneNumber:ok'){
					this.setData({
						infos: infos
					})
					this.getPhone();
				}
			}
		})
	},
	getPhone(){
		if(this.data.inofs !== null && this.data.token !== null){
			let infos = this.data.infos
			app.request({
				url: '/app/binding',
				method: 'POST',
				data: infos,
				success: res => {
					if(res.code == 200){
						this.setData({
							[`userInfo.phone`]: res.data.phone
						})
						this.getUserInfo();
					}else{
						//弹出错误信息
						wx.showToast({
							title: res.data.msg,
							icon:'error'
						})
					}
				},
				fail: err => {
					console.log(err)
				}
			})
		}
	},
	getUserProfile(e) {
		let that = this;
		wx.login({
			success: code => {
				this.setData({
					code: code.code
				})
			}
		})
		wx.getUserProfile({
			desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: info => {
				this.setData({
					[`userInfo.avatar`]: info.userInfo.avatarUrl,
					[`userInfo.nickName`]: info.userInfo.nickName
				})
				wx.showLoading({title: '加载中…'});
				app.request({
					url: '/app/login',
					method: 'POST',
					data: {
						code: this.data.code,
						iv: info.iv,
						signature: info.signature,
						encryptedData: info.encryptedData,
					},
					success: res => {
						wx.setStorageSync('token',res.token);
						this.setData({
							token: res.token
						})
						this.getPhone();
					}
				})
				wx.hideLoading();
			}
		})
	},
})
